import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
class Base():
    def __init__(self,driver):
        self.driver=driver
    def goto(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    def click(self,loc):
        self.find_element(loc).click()
    def test(self,loc):
        return self.find_element(loc).text
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    def move_loc(self,loc):
        ActionChains(self.driver).move_to_element(loc).perform()
    def drop_loc(self,loc):
        ActionChains(self.driver).drag_and_drop(loc).perform()
    def xlk(self,loc,x):
        select=Select(self.find_element(loc))
        select.select_by_value(x)
    def tk(self):
        tank=self.driver.switch_to.alert
        tank.accept()
    def jkj(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    def tkj(self):
        self.driver.switch_to.default_content()
#     页面切换
    def qh(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
#     键盘事件
    def hc(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
#     滚动
    def gd(self):
        self.driver.execute_script('window.scrollTo(0,1000)')
    def ys(self):
        self.driver.implicitly_wait(10)
    def sleep(self,timeout):
        time.sleep(int(timeout))
    def jt(self):
        times=time.strftime('%Y%m%d_%H%M%S',time.localtime())
        self.driver.save_screenshot('hj'+times+'.png')

